home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 41.zip / BS1 part 41 / Amiga Plus 1.adf / Programming / samplemain.c < prev    next >
C/C++ Source or Header  |  1978-04-08  |  9KB  |  270 lines

  1. /* :ts=8 */
  2. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
  3. /*                                                              */
  4. /*      SampleMain.c                                            */
  5. /*                                                              */
  6. /*      Main program for AmigaPLUS article about Intuition     */
  7. /*      Written by Michael G. Lehman                            */
  8. /*      of Intuitive Technologies                               */
  9. /*                                                              */
  10. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
  11.  
  12.  
  13. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
  14. /*      Complete Include file for Amiga programs                */
  15. /*      Derived from examples supplied by Commodore Amiga       */
  16. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
  17.  
  18.  
  19. #include <exec/types.h>
  20. #include <exec/nodes.h>
  21. #include <exec/lists.h>
  22. #include <exec/memory.h>
  23. #include <exec/ports.h>
  24. #include <exec/tasks.h>
  25. #include <exec/libraries.h>
  26. #include <exec/devices.h>
  27. #include <exec/io.h>
  28. #include <exec/devices.h>
  29.  
  30. #include <libraries/dos.h>
  31.  
  32. #include <graphics/gfx.h> /* ALWAYS INCLUDE GFX.H before other includes */
  33. #include <graphics/display.h>
  34. #include <graphics/clip.h>
  35. #include <graphics/rastport.h>
  36. #include <graphics/gfxbase.h>
  37. #include <graphics/text.h>
  38. #include <graphics/regions.h>
  39. #include <graphics/copper.h>
  40. #include <graphics/gels.h>
  41.  
  42. #include <devices/inputevent.h>
  43. #include <devices/gameport.h>
  44. #include <devices/console.h>
  45. #include <devices/keymap.h>
  46.  
  47. #include <intuition/intuition.h>
  48.  
  49.  
  50. extern UBYTE * AllocMem();
  51.  
  52. #define SECONDS io_Actual
  53. #define MICROSECONDS io_Length
  54.  
  55. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
  56. /*                                                              */
  57. /*      Global variables                                        */
  58. /*                                                              */
  59. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
  60.  
  61. long GfxBase = {0};             /* storage for pointers from OpenLibrary */
  62. long DiskfontBase=0;
  63. long IntuitionBase=0;
  64. long LayersBase = 0;
  65.  
  66. struct Window *OpenWindow();
  67. struct InputEvent *Intuition();
  68. struct Screen *OpenScreen();
  69.  
  70. extern struct MsgPort *CreatePort();
  71. extern struct IOStdReq *CreateStdIO();
  72.  
  73. struct IOStdReq *message2;              /* used for calling getmsg */
  74.  
  75.  
  76. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
  77. /*                                                              */
  78. /*      These variables are used to wait for and get info       */
  79. /*      From Intuition                                          */
  80. /*                                                              */
  81. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
  82.  
  83.  
  84. ULONG wakeupbit;        /* timer bit or event bit wake us up? */
  85. ULONG waitbits;         /* global wait bits, updated by make/shut window */
  86.  
  87.  
  88. USHORT class;                   /* type of message */
  89. USHORT code;                    /* modifier        */
  90. USHORT qualifier;               /* such as shift, alt keys */
  91. USHORT mousex;                  /* horizontal mouse position */
  92. USHORT mousey;                  /* vertical   mouse position */
  93.  
  94. USHORT mode;            /* mode of operation */
  95. APTR address;           /* address of the gadget which we hit */
  96.  
  97.  
  98. struct Screen *screen;
  99. struct RastPort *rp;
  100. struct ViewPort *vp;
  101. struct IntuiMessage *message;
  102. struct Message *msg;
  103. struct Process *my_process;
  104.  
  105. struct Window *window;
  106.  
  107. struct XMenuItem                /* our extended menu structure */
  108. {
  109.         struct MenuItem *NextItem;
  110.         SHORT LeftEdge, TopEdge;
  111.         SHORT Width, Height;
  112.         USHORT Flags;
  113.         LONG MutualExclude;
  114.         APTR ItemFill;
  115.         APTR SelectFill;
  116.         BYTE Command;
  117.         struct MenuItem *SubItem;
  118.         USHORT NextSelect;
  119.         int (*ItemHndlr)();     /* all above is standard and we add this */
  120. };
  121.  
  122. struct Menu *InitMenu();
  123.  
  124.  
  125.                         /****************************************/
  126. extern HandleEvent();   /* returns TRUE if we should quit       */
  127.                         /****************************************/
  128.  
  129.  
  130. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
  131. /*                                                              */
  132. /*      The M A I N program                                     */
  133. /*                                                              */
  134. /*      In this version we keep it simple.  Next time we will   */
  135. /*      add the ability to pass arguments (projects) into our   */
  136. /*      application and determine if they came from the CLI or  */
  137. /*      Workbench, setup directories, etc.                      */
  138. /*                                                              */
  139. /*                                                              */
  140. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
  141. /*      But for now:                                            */
  142. /*                                                              */
  143. /*      1.      Initialize environment                          */
  144. /*                                                              */
  145. /*      2.      Build menu strip                                */
  146. /*                                                              */
  147. /*      3.      Open Window                                     */
  148. /*                                                              */
  149. /*      4.      Handle events until quit                        */
  150. /*                                                              */
  151. /*      5.      Clear menu strip                                */
  152. /*                                                              */
  153. /*      6.      Close Window                                    */
  154. /*                                                              */
  155. /*      7.      Shutdown environment                            */
  156. /*                                                              */
  157. /*      8.      And exit                                        */
  158. /*                                                              */
  159. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
  160.  
  161.  
  162. main(argc,argv)
  163. int argc;
  164. char **argv;
  165. {
  166.  
  167.  
  168.         GfxBase = OpenLibrary("graphics.library", 0);
  169.         if (GfxBase == NULL)
  170.         {
  171.                 exit();
  172.         }
  173.         IntuitionBase = OpenLibrary("intuition.library", 0);
  174.         if (IntuitionBase == NULL)
  175.         {
  176.                 exit();
  177.         }
  178.         DiskfontBase = OpenLibrary("diskfont.library", 0);
  179.         if (DiskfontBase == NULL)
  180.         {
  181.                 exit();
  182.         }
  183.         LayersBase = OpenLibrary("layers.library",0);
  184.         if (LayersBase == NULL)
  185.         {
  186.                 exit();
  187.         }
  188.  
  189.         InitAppl();             /* call appl specific init code */
  190.  
  191.         /*---------------------------------*/
  192.         /* The call above opens our window */
  193.         /*---------------------------------*/
  194.  
  195.         class=0;  code =0;  /* initialize */
  196.  
  197.         wakeupbit = 0;
  198.  
  199.         /*----------------------------------------------*/
  200.         /* InitMenu returns a pointer to our menu strip */
  201.         /*----------------------------------------------*/
  202.  
  203.         SetMenuStrip(window, InitMenu(window->WScreen,window));
  204.  
  205. /*--------------------------------------------------------------------*/
  206. /* get the signal bit we wait on (for Intuition to send us a message) */
  207. /*--------------------------------------------------------------------*/
  208.  
  209.         waitbits  = (1 << window -> UserPort -> mp_SigBit);
  210.  
  211. again:
  212.  
  213.         /*==================================*/
  214.         /* W A I T  F O R  A  M E S S A G E */
  215.         /*==================================*/
  216.  
  217.        wakeupbit = Wait(waitbits);
  218.  
  219.  
  220.        if(window && (wakeupbit & (1 << window -> UserPort -> mp_SigBit)))
  221.           {
  222.  
  223.             while(window &&
  224.              (message = (struct IntuiMessage *)GetMsg(window->UserPort)))
  225.             {
  226.                 /******************************************/
  227.                 /* Extract the message from the structure */
  228.                 /* because we may need to send the reply  */
  229.                 /* back immediately                       */
  230.                 /******************************************/
  231.  
  232.               class = message->Class;
  233.               code =  message->Code;
  234.               qualifier = message->Qualifier;
  235.               mousex = message->MouseX;
  236.               mousey = message->MouseY;
  237.               address = message->IAddress;
  238.  
  239.               if (class != MENUVERIFY)
  240.                 {
  241.                   ReplyMsg(message);            /* send reply right away */
  242.                   if (HandleEvent())
  243.                     goto exit_program;
  244.                 }
  245.               else
  246.                 {
  247.                   if (HandleEvent())
  248.                       goto exit_program;      /* the user selected quit */
  249.                   ReplyMsg(message);
  250.                 }
  251.  
  252.             } /* end while */
  253.           }
  254.  
  255.         goto again;
  256.  
  257. exit_program:
  258.  
  259.  
  260.         UnMakeMenuStrip(window->MenuStrip);
  261.         ClearMenuStrip(window);
  262.         CloseWindow(window);
  263.  
  264.         CloseLibrary(LayersBase);
  265.         CloseLibrary(DiskfontBase);
  266.         CloseLibrary(IntuitionBase);
  267.         CloseLibrary(GfxBase);
  268.   exit(0);
  269. }
  270.